﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class CompanyProducts : YuanlinUserControl,
        IReplyConverter<CompanyProductReply, GetListOperation>,
        IReplyConverter<HuamuAndCategoryRelationReply, GetListOperation>
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        protected Button btnQty;
        protected Button btnPrice;
        protected Button btnCreateDate;
        private EntityList companyProducts;
        private EntityList companyProduct1s;
        public int categoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryId);
        public int appCategoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.AppCategoryId);
        public string tradeType = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.TradeType);
        public int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);
        public string orderBy = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.OrderBy);
        public string ascStr = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.Asc);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!IsPostBack)
            {
                HuamuAndCategoryRelationListRequest relationRequest = new HuamuAndCategoryRelationListRequest();
                relationRequest.CategoryId = appCategoryId;
                relationRequest.PageIndex = 0;
                relationRequest.PageSize = Int32.MaxValue;
                requestBinders.Add(RequestHelper.BuildGetHuamuAndCategoryRelationListRequest(this, relationRequest));

                CompanyProductListRequest request = new CompanyProductListRequest();
                request.UserId = UserIdFromGreenNum;
                request.CategoryId = categoryId;
                request.TradeType = tradeType;
                request.Verify = 1;
                request.PageIndex = pager.PageIndex;
                request.PageSize = pager.PageSize;

                if (string.IsNullOrEmpty(orderBy))
                {
                    requestBinders.Add(RequestHelper.BuildGetCompanyProductListRequest(this, request));
                }
                else
                {
                    int asc = string.IsNullOrEmpty(ascStr) ? 0 : Globals.ChangeType<int>(ascStr);
                    requestBinders.Add(RequestHelper.BuildGetCompanyProductListRequest(this, request, orderBy, asc));
                }
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            if (btnQty != null)
            {
                btnQty.Click += new EventHandler(btnQty_Click);
            }
            if (btnPrice != null)
            {
                btnPrice.Click += new EventHandler(btnPrice_Click);
            }
            if (btnCreateDate != null)
            {
                btnCreateDate.Click += new EventHandler(btnCreateDate_Click);
            }
        }

        protected void btnQty_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ascStr))
            {
                ascStr = "0";
            }
            else
            {
                ascStr = ascStr == "0" ? "1" : "0";
            }
            Response.Redirect(SiteUrls.Instance().GetCompanyProductUrl(tradeType, categoryId, appCategoryId, 2, categoryType, "Number", ascStr));
        }
        protected void btnPrice_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ascStr))
            {
                ascStr = "0";
            }
            else
            {
                ascStr = ascStr == "0" ? "1" : "0";
            }
            Response.Redirect(SiteUrls.Instance().GetCompanyProductUrl(tradeType, categoryId, appCategoryId, 2, categoryType, "Price", ascStr));
        }
        protected void btnCreateDate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ascStr))
            {
                ascStr = "0";
            }
            else
            {
                ascStr = ascStr == "0" ? "1" : "0";
            }
            Response.Redirect(SiteUrls.Instance().GetCompanyProductUrl(tradeType, categoryId, appCategoryId, 2, categoryType, "UpdateDate", ascStr));
        }
        private void BindData()
        {
            EntityList tempList = new EntityList();
            foreach (CompanyProduct item in companyProducts)
            {
                foreach (HuamuAndCategoryRelation relation in companyProduct1s)
                {
                    if (relation.HuamuId.Value == item.EntityId.Value)
                    {
                        tempList.Add(item);
                    }
                }
            }

            list.DataSource = tempList;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(tempList.TotalCount);
                currentPage.TotalRecords = (tempList.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        /// <summary>
        /// 得到字典列表（首页上的那个七大版块）
        /// </summary>
        /// <param name="companyProductId">公司花木Id</param>
        /// <returns>字典Url</returns>
        public string GetDicUrl(int productId)
        {
            if (productId == 0)//如果与网站的花木没对应上的就不用返回链接
            {
                return "javascript:void(0);";
            }

            return SiteUrls.Instance().GetTechUrl(productId,(int)CategoryType.Huamu);
        }
        /// <summary>
        /// 要显示的规格和单位，在维护的时候会在单位的最后加个*(这是和客户约定好的)
        /// </summary>
        /// <param name="categoryId">植物角度分类</param>
        /// <param name="flag">0:显示规格数据，1:显示单位数据</param>
        /// <returns></returns>
        public string GetProductSpecification(int categoryId, int flag)
        {
            ProductSpecificationListRequest request = new ProductSpecificationListRequest();
            request.ProductCategoryId = categoryId;
            request.Unit = "*";
            EntityList productSpecifications = BusinessManager.GetProductSpecificationList(RequestHelper.BuildGetProductSpecificationListRequest(request));
            return GetResult(productSpecifications, flag);
        }
        private string GetResult(EntityList productSpecifications, int flag)
        {
            string returnvalue = string.Empty;
            string specification = string.Empty;
            string unit = string.Empty;
            if (productSpecifications == null || productSpecifications.Count == 0)
            {
                return returnvalue;
            }
            foreach (ProductSpecification item in productSpecifications)
            {
                specification += "," + item.Title;
                unit += "," + item.Unit.Value.Replace("*", "");
            }
            switch (flag)
            {
                case 0:
                    if (specification.Length > 0)
                    {
                        returnvalue = specification.Substring(1);
                    }
                    break;
                case 1:
                    if (unit.Length > 0)
                    {
                        returnvalue = unit.Substring(1);
                    }
                    break;
            }
            return returnvalue;
        }

        public string GetTarget(int companyProductId)
        {
            Product product = BusinessManager.GetProduct(companyProductId);
            if (product == null)//如果与网站的花木没对应上的就不用返回链接
            {
                return string.Empty;
            }
            return "_blank";
        }

        #region IReplyConverter<CompanyProductReply,GetListOperation> Members

        public void ConvertReply(CompanyProductReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                companyProducts = reply.EntityList;

                if (!IsPostBack)
                {
                    BindData();
                }
            }
        }

        #endregion

        #region IReplyConverter<HuamuAndCategoryRelationReply,GetListOperation> 成员

        public void ConvertReply(HuamuAndCategoryRelationReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                companyProduct1s = reply.EntityList;
            }
        }

        #endregion
    }
}

